// pages/repairlist/repairlist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [],
    multiIndex: 0,
    showFilePath: "",
    ownerName:'',
    communityName:'',
    userInfo:[]
  },

  uploadImage() {
    let that=this;
    let fileArrays = [];
    wx.chooseMedia({
      success: function (res) {
        console.log(res);
        // tempFiles  数组  tempFilePath 数组图片路径
        let localFiles = res.tempFiles;
  
        wx.showLoading({
          title: '正在上传',
        })
        console.log("$$$$$$$")
        console.log(localFiles.length)
        console.log("$$$$$$$")
        //数组遍历
        for (let i = 0; i < localFiles.length; i++) {
          let localPath = localFiles[i].tempFilePath;
            fileArrays.push(localPath)
            console.log(fileArrays);
            // that.setData({
            //   showFilePath:fileArrays
            // })
          wx.uploadFile({ 
            url:'http://localhost/file',  
            header: { "Content-Type": "multipart/form-data" },
            name:'file',
            filePath: localPath, 
            success: function (res) {
              wx.showToast({
                title: '上传成功',
              })
              console.log("$$$$$$$")
              that.setData({
                showFilePath:""+res.data
              })
              console.log(that.data.showFilePath)
              console.log(res.data);
            console.log("$$$$$$$")
            },
            fail(error){
              wx.showToast({
                title: '上传失败',
              })
            }
          })
        }
      }
    })
  },
  previewImg:function(e){
    var that=this;
    var src=e.currentTarget.dataset.src;
    
    wx.previewImage({
      current:src,
      urls: [that.data.showFilePath],
    })
  },
  removeImage(e){
    this.setData({
      showFilePath:''
    })

  },

  formSubmit(e){
    if(this.data.userInfo.ownerName){
     
    let communityName=this.data.communityName;
    let ownerName=this.data.ownerName;
    let buildingName=this.data.multiArray[this.data.multiIndex];
    let repairImage=this.data.showFilePath;
    let deviceName=e.detail.value.deviceName;
    let description=e.detail.value.description;
      // 当前日期时间  YYYY-MM-DD hh:mm:ss    
   let createTime=new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8); 
    wx.request({
      url: 'http://localhost/repair/addRepair',
      data:{
        communityName:communityName,
        ownerName:ownerName,
        buildingName:buildingName,
        repairImage:repairImage,
        deviceName:deviceName,
        description:description,
        createTime:createTime,
        status:0
      },
      success(result){
        console.log(result);
            wx.navigateTo({
          url: '../repair/repair',
        })
           if(result.data.code==0){  
           wx.showToast({
             icon:'success',
             title: result.data.msg,         
           })
     
         }else{
           wx.showToast({
             icon:'error',
             title: result.data.msg,
           })
         }
 
       }
     })
    }else{
      wx.navigateTo({
        url: '../userBind/userBind',
      })
      wx.showToast({   
        icon:'none',
        title: '请认证您的身份',
      })
    }
    
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that=this;
    // 页面初始化 options为页面跳转所带来的参数
    let userInfo=wx.getStorageSync('userInfo');
     if(userInfo){
       userInfo =JSON.parse(userInfo);
      this.setData({
        ownerName:userInfo.ownerName,
        communityName:userInfo.communityName,
        userInfo:userInfo
      })
      console.log(this.data.ownerName);
    }

    wx.request({
      url: 'http://localhost/repair/getBuildingName',
      success:function(result){
        console.log(result.data);
        that.setData({
          multiArray:result.data
        })
      }
    });
  },
  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
    })
    console.log(this.data.multiArray[this.data.multiIndex]);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})